﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ThridGroup.ERP.DTO;
using ThridGroup.ERP.ProductionSystem;

namespace ThridGroup.ERP.Web.Controllers.ProductionSystem
{
    /// <summary>
    /// 
    /// </summary>
    public class ItemSettingsController : Controller
    {
        private readonly IItemSettingsServices _services;

        public ItemSettingsController(IItemSettingsServices sservices)
        {
            _services = sservices;
        }

        public IActionResult ItemSettingsView()
        {
            return View();
        }

        public IActionResult UpdateOrInsertItemSettingsView()
        {
            return View();
        }

        /// <summary>
        /// 添加检测项设置
        /// </summary>
        /// <param name="itemSettings"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddItemSettings([FromBody] ItemSettingsDTO itemSettings)
        {
            return await _services.AddItemSettings(itemSettings);
        }

        /// <summary>
        /// 删除检测项设置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> DelItemSettings(int id)
        {
            return await _services.DelItemSettings(id);
        }

        /// <summary>
        /// 批量删除检测项设置
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task DelMany([FromBody] List<int> ids)
        {
            await _services.DelMany(ids);
        }

        /// <summary>
        /// 检测项设置列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ItemSettingsDTO> GetItemSettings(int id)
        {
            return await _services.GetItemSettings(id);
        }

        /// <summary>
        /// 检测项设置列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="itemSettingsName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> ShowItemSettings(int pageIndex = 1, int pageSize = 10, string? itemSettingsName = "")
        {
            var list = await _services.ShowItemSettings(pageIndex, pageSize, itemSettingsName);
            var result = list.OrderByDescending(m => m.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return Json(new
            {
                code = 0,
                msg = "",
                count = list.Count,
                data = result

            });
        }

        /// <summary>
        /// 编辑检测项设置
        /// </summary>
        /// <param name="itemSettings"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateItemSettings([FromBody]ItemSettingsDTO itemSettings)
        {
            return await _services.UpdateItemSettings(itemSettings);
        }


    }
}
